#!/bin/sh

#-------------------------------------#
#  SGT (Software Generation Toolkit)  #
#-------------------------------------#
#                                     #
#      Compilation script for         #
#         a single file               #
#                                     #
#  GDE 03/07/2003 v1.8                #
#       check \MAK subdirectory       #
#                                     #
#  GDE 22/04/2003 v1.7                #
#       correct FILE_SRC definition   #
#                                     #
#  GDE 18/12/2002 v1.6                #
#       add verification on           #
#       the second parameter          #
#                                     #
#  GDE 15/07/2002 v1.5                #
#       update rules name             #
#                                     #
#  GDE 07/06/2002 v1.4                #
#       delete MSG_ERROR_SCRIPT=2     #
#       for the second parameter      #
#                                     #
#  GDE 14/05/2002 v1.3                #
#       update for SGT 1.1.1          #
#                                     #
#  GDE 07/05/2002 v1.2                #
#       update for SGT 1.1.0          #
#                                     #
#  GDE 22/03/2002 v1.1                #
#       update the version            #
#        and help sections            #
#                                     #
#  GDE 20/12/2001 v1.0                #
#       creation                      #
#                                     #
#-------------------------------------#
# $1: target makefile / target file   #
# $2: target file / specific option   #
# $3: specific options                #
# $4-9: other options                 #
#-------------------------------------#



#-------------------------------------#
# Script informations
#-------------------------------------#
export SCRIPT_SGT_VER=1.8
export SCRIPT_SGT_DATE=03/07/2003
export SCRIPT_SGT_USER=GDE
export SCRIPT_SGT_NAME=compile
export SCRIPT_SGT_USAGE="Compile the current input file"
#-------------------------------------#



#---------------------
# Create and go to output directory
#---------------------
if [ -d mak ]
then
	mkdir -p -m775 out
	cd out
else
	export MSG_ERROR_SCRIPT=0
	export CMD_SCRIPT=Error
fi
#---------------------



#-------------------------------------#
# Parameters section
#-------------------------------------#
#---------------------
# Parameter 3 -> specific option
#---------------------
if [ ! -z $3 ] && [ -z $CMD_SCRIPT ]
then
	if [ $3 = -w ]
	then
		export OPTION_MAK=WARNING=OFF
	fi
	# else -> $3 is a custom argument
fi


#---------------------
# Parameter 2 -> file to compile / specific option
#---------------------
if [ ! -z $2 ] && [ -z $CMD_SCRIPT ]
then
	if [ $2 = -w ]
	then
		export OPTION_MAK=WARNING=OFF
	else
		if [ -f $2 ]
		then
			export FILE_SRC=$2
	fi
		# else -> $2 is a custom argument
	fi
fi


#---------------------
# Parameter 1 -> target file / file to compile / specific option
#---------------------
if [ ! -z $1 ] && [ -z $CMD_SCRIPT ]
then
	if [ $1 = -w ]
	then
		export OPTION_MAK=WARNING=OFF
	else
		if [ $1 = -v ]
		then
			export CMD_SCRIPT=Version
		else
			if [ $1 = -h ]
			then
				export CMD_SCRIPT=Help
			else
				if [ $1 = --help ]
				then
					export CMD_SCRIPT=Help
				else
					if [ $1 = -hm ]
					then
						export CMD_SCRIPT=More
					else
						if [ $1 = -u ]
						then
							export CMD_SCRIPT=Usage
						else
							if [ -f ../mak/$1.mak ]
							then
								export TARGET_LIB=$1
								export FILE_SRC=$2
							else
								if [ -f $1 ] && [ -z $FILE_SRC ]
								then
									export FILE_SRC=$1
								else
									export MSG_ERROR_SCRIPT=1
									export CMD_SCRIPT=Error
								fi
							fi
						fi	
					fi
				fi
			fi
		fi
	fi
fi


#-------------------------------------#
# if no option is defined, display help
#-------------------------------------#
if [ $# -eq 0 ] && [ -z $CMD_SCRIPT ]
then
	export CMD_SCRIPT=Help
fi


#-------------------------------------#
# Process section
#-------------------------------------#
#---------------------
# Mode 1 -> compil <target> <file> <option>
#---------------------
if [ ! -z $TARGET_LIB ] && [ ! -z $FILE_SRC ] && [ -z $CMD_SCRIPT ]
then
	echo " "
	echo "    $SCRIPT_SGT_USAGE"
	echo "    Check dependencies for $TARGET_LIB library"
	echo " "
	if [ -z $OPTION_MAK ]
	then
		make -f ../mak/gen.mak compile_file TARGET=$TARGET_LIB SRC_FILE=$FILE_SRC DEPENDENCIES=ON $3 $4 $5 $6 $7 $8 $9
	else
		make -f ../mak/gen.mak compile_file TARGET=$TARGET_LIB SRC_FILE=$FILE_SRC $OPTION_MAK DEPENDENCIES=ON $4 $5 $6 $7 $8 $9
	fi
	exit
fi

#---------------------
# Mode 2 -> compil <file> <option>
#---------------------
if [ ! -z $FILE_SRC ] && [ -z $CMD_SCRIPT ]
then
	echo " "
	echo "    $SCRIPT_SGT_USAGE"
	echo " "
	if [ -z $OPTION_MAK ]
	then
		make -f ../mak/gen.mak compile_file TARGET=misc PROCESS=lib USE_TARGET_FILE=OFF SRC_FILE=$FILE_SRC $2 $3 $4 $5 $6 $7 $8 $9
	else
		make -f ../mak/gen.mak compile_file TARGET=misc PROCESS=lib USE_TARGET_FILE=OFF SRC_FILE=$FILE_SRC $OPTION_MAK $3 $4 $5 $6 $7 $8 $9
	fi
	exit
fi



#-------------------------------------#
# Help script section
#-------------------------------------#
if [ -z $CMD_SCRIPT ]
then
	export CMD_SCRIPT=Version
fi

#---------------------
# Usage
#---------------------
if [ $CMD_SCRIPT = Usage ]
then
	echo "  $SCRIPT_SGT_NAME	$SCRIPT_SGT_USAGE"
	exit
fi

#---------------------
# Version
#---------------------
echo " "
echo "-------------------------------------"
echo " '$SCRIPT_SGT_NAME' (release "$SCRIPT_SGT_VER") for SGT "$SGT_VER". Last update by "$SCRIPT_SGT_USER" on "$SCRIPT_SGT_DATE
echo "  $SCRIPT_SGT_USAGE"
if [ $CMD_SCRIPT = Version ]
then
	exit
fi

#---------------------
# Simple help
#---------------------
if [ $CMD_SCRIPT = Help ]
then
	echo "-------------------------------------"
	echo " $SCRIPT_SGT_NAME [LIBRARY] FILE [-OPTION]"
	echo "  When LIBRARY is specified, SGT check"
	echo "  dependencies before compile FILE."
	echo " "
	echo "  -w  Disable some warning levels"
	echo " "
	echo "  -h | --help  Display this help "
	echo "  -hm          Display more help about SGT" $SGT_VER
	echo "  -v           Display the version of the current script"
fi

#---------------------
# More help
#---------------------
if [ $CMD_SCRIPT = More ]
then
	make -f ../mak/gen.mak help USE_TARGET_FILE=OFF
fi



#-------------------------------------#
# Error messages section
#-------------------------------------#
if [ $CMD_SCRIPT = Error ]
then
	if [ $MSG_ERROR_SCRIPT = 0 ]
	then
		echo "-------------------------------------"
		echo " "
		echo "ERROR : The root path is not a valid SGT directory. Please check for the \MAK sub-directory"
	fi
	if [ $MSG_ERROR_SCRIPT = 1 ]
	then
		echo "-------------------------------------"
		echo " "
		echo "ERROR : $1 or ../mak/$1.mak doesn't exist !"
	fi
	echo " "
	echo " Check your command line or see help section -> $SCRIPT_SGT_NAME -h"
fi